unit Unit1;
//AnsiString
interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, rtcDataSrv, rtcInfo, rtcConn, rtcHttpSrv,
  Vcl.StdCtrls, rtcDataCli, rtcHttpCli;

type
  TForm1 = class(TForm)
    RtcDataProvider1: TRtcDataProvider;
    Btnx_Upload: TButton;
    RtcHttpClient1: TRtcHttpClient;
    HtpSrv_1: TRtcHttpServer;
    RtcDataRequest1: TRtcDataRequest;
    Chkb_1: TCheckBox;
    Btnx_Obtain: TButton;
    procedure RtcDataProvider1CheckRequest(Sender: TRtcConnection);
    procedure RtcDataProvider1DataReceived(Sender: TRtcConnection);
    procedure Btnx_UploadClick(Sender: TObject);
    procedure RtcDataRequest1BeginRequest(Sender: TRtcConnection);
    procedure Chkb_1Click(Sender: TObject);
    procedure RtcDataRequest1DataSent(Sender: TRtcConnection);
    procedure Btnx_ObtainClick(Sender: TObject);
    procedure RtcDataRequest1DataReceived(Sender: TRtcConnection);
  private
    FOutCount:Integer;
    FIntCount:Integer;

    TargetName:string;
    TargetPath:string;
  public

  end;

var
  Form1: TForm1;

implementation

uses
  Class_KzUtils,Class_KzDebug;

{$R *.dfm}

procedure TForm1.Btnx_ObtainClick(Sender: TObject);
var
  TargetName:string;
  TargetPath:string;
begin
  TargetName:='DEMO.EXE';
  TargetPath:=ExtractFilePath(ParamStr(0))+'OBTAIN\DEMO.EXE';

  with RtcHttpClient1 do
  begin
    ServerAddr:='localhost';
    ServerPort:='9090';
    Connect();
  end;

  with RtcDataRequest1 do
  begin
    Request.Query['OBTAIN_TARGET']  :=  URL_Encode(TargetName);
    Request.Info ['OBTAIN_SOURCE']  :=  TargetPath;

    Request.Method  :='POST';
    Request.FileName:='/OBTAIN';

    Post();
  end;
end;

procedure TForm1.Btnx_UploadClick(Sender: TObject);
var
  OD:TOpenDialog;
begin
  try
    OD:=TOpenDialog.Create(nil);
    if OD.Execute then
    begin
      TargetPath:=OD.FileName;
      TargetName:=ExtractFileName(TargetPath);

      FOutCount:=0;
      FIntCount:=0;

      with RtcHttpClient1 do
      begin
        ServerAddr := 'localhost';
        ServerPort := '9090';
        if isConnected then
        begin
          Disconnect;
        end;
        Connect();
      end;

      with RtcDataRequest1 do
      begin
        Request.Query.Clear;
        Request.Query['UPLOAD_TARGET'] := URL_Encode((TargetName));

        Request.Info.Clear;
        Request.Info ['UPLOAD_SOURCE'] := TargetPath;
        KzDebug.FileFmt('UPLOAD_SOURCE:%S',[TargetPath]);
        KzDebug.FileFmt('UPLOAD_SOURCE:%S',[Request.Info['UPLOAD_SOURCE']]);

        Request.Method   := 'POST';
        Request.FileName := '/UPLOAD';
        Request.ContentLength :=File_Size(Request.Info.asString['UPLOAD_SOURCE']);

        Post;
      end;
    end;
  finally
    FreeAndNil(OD);
  end;
end;

procedure TForm1.Chkb_1Click(Sender: TObject);
begin
  if TCheckBox(Sender).Checked then
  begin
    with HtpSrv_1 do
    begin
      ServerAddr:='localhost';
      ServerPort:='9090';
      Listen();
    end;
  end else
  begin
    with HtpSrv_1 do
    begin
      StopListen;
    end;
  end;
end;

procedure TForm1.RtcDataProvider1CheckRequest(Sender: TRtcConnection);
begin
  with TRtcDataServer(Sender) do
  begin
    if (Request.FileName='/UPLOAD') then
    begin
      Accept;
    end else
    if (Request.FileName='/OBTAIN') then
    begin
      Accept;
    end;
  end;
end;

procedure TForm1.RtcDataProvider1DataReceived(Sender: TRtcConnection);
var
  Value: string;
  TMPA : RtcString;
begin
  with TRtcDataServer(Sender) do
  begin
    if UpperCase((Request.FileName)) = '/UPLOAD' then
    begin
      if Request.Started then
      begin
        TMPA:=ExtractFilepath(ParamStr(0)) + 'UPLOAD\'+ string(URL_Decode(Request.Query['UPLOAD_TARGET']));
        Request.Info['UPLOAD_TARGET']:=TMPA;

        if not DirectoryExists(ExtractFilepath(ParamStr(0)) + 'UPLOAD') then
        begin
          CreateDir(ExtractFilepath(ParamStr(0)) + 'UPLOAD');
        end;
        Delete_File(Request.Info['UPLOAD_TARGET']);
      end;
      TMPA:=Request.Info.asString['UPLOAD_TARGET'];

      Value := Read;
      FIntCount:=FIntCount + Length(Value);
      Write_File(TMPA,Value,Request.ContentIn - length(Value));
    end else
    if UpperCase(Request.FileName)='/OBTAIN' then
    begin
      if Request.Started then
      begin
        TMPA:=ExtractFilepath(ParamStr(0)) + 'UPLOAD\'+'demo.exe';
        Request.Info['OBTAIN_TARGET']:=TMPA;
      end;
      TMPA := Request.Info.asString['OBTAIN_TARGET'];
      if File_Exists(TMPA) then
      begin
        Value:=Read_File(TMPA);
        Write(Value);
      end;
    end;

    if Request.Complete then
    begin
      //Write;
    end;
  end;
end;

procedure TForm1.RtcDataRequest1BeginRequest(Sender: TRtcConnection);
begin
  with TRtcDataClient(Sender) do
  begin
    WriteHeader;
  end;
end;

procedure TForm1.RtcDataRequest1DataReceived(Sender: TRtcConnection);
var
  Value:string;
begin
  with TRtcDataClient(Sender) do
  begin
    if (Request.FileName='/OBTAIN') then
    begin
      if Response.Started then
      begin
        if not DirectoryExists(ExtractFilepath(ParamStr(0)) + 'OBTAIN') then
        begin
          CreateDir(ExtractFilepath(ParamStr(0)) + 'OBTAIN');
        end;
        Delete_File(Request.Info['OBTAIN_SOURCE']);
      end;


      Value := read;
      FIntCount:=FIntCount + Length(Value);
      Write_File(Request.Info.asString['OBTAIN_SOURCE'],Value,Response.ContentIn - Length(Value));
    end;
  end;
end;

procedure TForm1.RtcDataRequest1DataSent(Sender: TRtcConnection);
var
  bSize, j: Int64;
  TMPA:string;
begin
  with TRtcDataClient(Sender) do
  begin
    if Request.ContentLength > Request.ContentOut then
    begin
      j := 1024 * 64;
      bSize := Request.ContentLength - Request.ContentOut;
      if bSize > j then
      begin
        bSize := j;
      end;
      FOutCount:=FOutCount + bSize;
      TMPA:=Request.Info['UPLOAD_SOURCE'];
      KzDebug.FileFmt('UPLOAD_SOURCE:%S',[TMPA]);
      Write(Read_File(TMPA,Request.ContentOut, bSize));
    end;
  end;
end;

end.
